#客户端  test


# server_url="http://172.21.16.42:8008"
import base64
import json
import os
import shutil
import cv2
import requests
# from tools.infer import clas_main



class w_client():
    def save_dir_clas_img(self,label_name,img_path,score):
        """##测试后按规则保存"""
        #保存结果路径
        test_img_path="/backup/shuyan/test_data/small_img_test_dir/paddle_cls_pplcnet07"
        test_img_path=os.path.join(test_img_path,label_name)
        img_name=str(score)+"__"+img_path.split("__")[-1]
        os.makedirs(test_img_path,exist_ok=True)
        new_img_path=os.path.join(test_img_path,img_name)
        shutil.copy(img_path,new_img_path)


    def clas_fun_swin(self,img_src_img):
        server_url="http://172.21.16.42:9902"
        header ={"Content-Type": "application/x-www-form-urlencoded"}
        # image = cv2.imread("./out/dividin--dividinge__dividing__44373_10448_107647_1790698_1_10448_oyu3N0VvSqav63qRhvRuHB7alYT4_1606649492712.jpg")
        # image = cv2.imread("/backup/shuyan/PROJECT/PaddleClas/english/test_img/not_resize/判错__test__crnn__totest--totest--tatest__0__2123_2106066_2_11346_oyu3N0UYFJLZhKtWhwy7jx7tS7UM_801_1670422665362c.jpg")
        # res=clas_main(image)

        answer = "learn to do"
        ret, base64_image = cv2.imencode(".jpg", img_src_img)
        base64_image = str(base64.b64encode(base64_image))[2:-1]

        payload = {
                    "image_base64": base64_image,
                    "answer": answer,
                    "pic_url": "test_test.py",
                    "mode": ""
                }
        # payload=json.dumps(payload)

        session = requests.session()
        session.keep_alive = False
        response = session.post(server_url + "/predict", data=payload, headers=header)
        result = response.text if response.status_code == 200 else response.status_code

        result=json.loads(result)
        # print(result,type(result))

        return result.get("res")



    def dir_img_test(self,dir_img_path):
        """ 
            整个文件夹测试  
        """
        # path_dir="/backup/shuyan/project/people_log/f_log_txt/FP_FN2137/判错/small_pic"
        path_dir=dir_img_path
        for dirpath, dirnames, filenames in os.walk(path_dir):
            # print('文件路径', dirpath)
            # print('文件夹名字', dirnames)
            for filename in filenames:
                path_dir_this=os.path.join(dirpath,filename)
                img_src_img=cv2.imread(path_dir_this)

                result=self.clas_fun_swin(img_src_img)
                label_name=result.get("res")[0].get("label_names")[0]
                score=result.get("res")[0].get("scores")[0]
                self.save_dir_clas_img(label_name,path_dir_this,score)


    def one_img_test(self,one_img_path):
        """
            单张测试
        """
        img_src_img = cv2.imread(one_img_path)
        # img_src_img = cv2.imread("/backup/shuyan/PROJECT/PaddleClas/english/test_img/not_resize/判错__test__crnn__totest--totest--tatest__0__2123_2106066_2_11346_oyu3N0UYFJLZhKtWhwy7jx7tS7UM_801_1670422665362c.jpg")
        result=self.clas_fun_swin(img_src_img)
        print("单张测试结果：",result)


client_web=w_client()
#测试单张图片
one_img_path=r""
client_web.one_img_test(one_img_path)


# #测试文件夹
# dir_img_path=r""
# client_web.dir_img_test(dir_img_path)